package com.project.controller;

import com.github.pagehelper.PageInfo;
import com.project.ResultVO;
import com.project.StringUtil;
import com.project.bean.SuppliersBean;
import com.project.mapper.SuppliersDto;
import com.project.service.ISuppliersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "客户测试接口")
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    private ISuppliersService supplierService;

//
//    //导出接口
//    @GetMapping("/exportExcel")
//    @ApiOperation(value = "导出excel")
//    public ResultVO export(HttpServletResponse response) {
////        if(!SecurityUtils.getSubject().isPermitted("/supplier")){
////            return ResultVO.hasNoPower();
////        }
//        System.out.println("开始导出");
//        List<SuppliersBean> supplierList = supplierService.findAll();
//        System.out.println("-------------------------------------------------------------------");
//        System.out.println(supplierList.size());
//        //设置序号（将id字段作为序号，导出后实现序号递增）
//        int i =1;
//        for(SuppliersBean supplier:supplierList){
//            supplier.setId(i++);
//        }
//        // 导出操作
//        ExcelUtils.exportExcel(supplierList, "(供应商表)",
//                "供应商表", SuppliersBean.class, "suppliers.xls", response);
//        return ResultVO.ResultVoOK("导出成功");
//    }
//
//    @PostMapping("/importExcel")
//    @ApiOperation(value = "导入excel")
//    public ResultVO importExcel2(@RequestParam("file") MultipartFile file) {
////        if(!SecurityUtils.getSubject().isPermitted("/supplier")){
////            return ResultVO.hasNoPower();
////        }
//        ImportParams importParams = new ImportParams();
//        // 数据处理
//        //表格标题行数,默认0
//        importParams.setHeadRows(1);
//        //表头行数,默认1
//        importParams.setTitleRows(0);
//        //是否需要校验上传的Excel,默认false
//        importParams.setNeedVerify(false);
//        int i = 0;
//        boolean flag = false;
//        try {
//            ExcelImportResult<SuppliersBean> result = ExcelImportUtil.importExcelMore(file.getInputStream(), SuppliersBean.class, importParams);
//            List<SuppliersBean> supplierList = result.getList();
//            for (SuppliersBean supplier : supplierList) {
//                flag = false;
//                if(supplierService.addSuppliers(supplier)){
//                    flag= true;
//                }
//                System.out.println("从Excel导入数据到数据库的详细为 ：{"+ JSONObject.toJSONString(supplier)+"}");
//                //TODO 将导入的数据做保存数据库操作,先将所有数据id设置为null
//            }
//            System.out.println("从Excel导入数据一共 {"+ supplierList.size()+"} 行 ");
//            i = supplierList.size();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResultVO.getResultVo(false,"导入成功","导入失败");
//        }
//        return ResultVO.getResultVo(flag,"导入成功,共导入 "+i+" 条数据","提交表单客户重复，请检查后再添加");
//    }


    @GetMapping("findByItem/{page}/{pageSize}/{name}/{range}")
    @ApiOperation(value = "按选项分页查找供应商")
    public PageInfo<SuppliersDto> findByItem(@PathVariable("page") int page,
                                            @PathVariable("pageSize") int pageSize,
                                            @PathVariable("name") String name,
                                            @PathVariable("range") String range){
        if(name.equals("$")){
            name = "";
        }
        if(range.equals("$")){
            range = "";
        }
        PageInfo<SuppliersDto> pageInfo = new PageInfo<>();
        BeanUtils.copyProperties(supplierService.findByItem(page,pageSize,name,range),pageInfo);
        return pageInfo;
    }

    @PostMapping("add")
    @ApiOperation(value = "添加供应商")
    public ResultVO add(@RequestBody SuppliersDto supplier){
        SuppliersBean suppliersBean = new SuppliersBean();
        BeanUtils.copyProperties(supplier,suppliersBean);
        return ResultVO.getResultVo(supplierService.addSuppliers(suppliersBean),
                StringUtil.ADDSUPPLIER_OK,
                StringUtil.ADDSUPPLIER_ERROR_EXIST);
    }

    @GetMapping("findById/{id}")
    @ApiOperation(value = "按id查询供应商")
    public SuppliersDto findById(@PathVariable("id") int id){
        SuppliersDto suppliersDto = new SuppliersDto();
        SuppliersBean suppliersBean =supplierService.findById(id);
        BeanUtils.copyProperties(suppliersBean,suppliersDto);
        return suppliersDto;
    }

//    @PutMapping("updates")
//    @ApiOperation(value = "修改供应商信息")
//    public SuppliersDto updateS(int id,String suppliersManager,String suppliersPhone){
//        SuppliersBean supplierBean = new SuppliersBean();
//        BeanUtils.copyProperties(suppliers,supplierBean);
//        supplierService.update(supplierBean);
//        return ResultVO.ResultVoOK(StringUtil.UPDATESUPPLIER_OK);
//    }

    @PutMapping("update")
    @ApiOperation(value = "修改供应商信息")
    public ResultVO update(@RequestBody SuppliersBean supplier){
        SuppliersBean suppliersBean = new SuppliersBean();
        BeanUtils.copyProperties(supplier,suppliersBean);
        supplierService.update(suppliersBean);
        return ResultVO.ResultVoOK(StringUtil.UPDATESUPPLIER_OK);
    }

//    @GetMapping("findAll")
//    @ApiOperation(value = "查询所有供应商")
//    public List<SuppliersBean> findAll(){
//        return supplierService.findAll();
//    }

}
